from PyQt4 import QtCore

class ViewDisplayerError(Exception):
    '''Exception raised by the `ViewDisplayer`s components.'''
    
    pass


def is_display(obj):
    '''Returns True if *obj* has the *ViewDisplayer* interface.'''

    try:
        for attr in dir(AbstractViewDisplayer):
            if attr.startswith('_'):
                continue
            if not hasattr(obj, attr):
                return False
    except Exception, e:
        return False
    return True

def ViewDisplayer(add_view, close_view):
    '''Decorator that can be used to make a class
    have the ViewDisplayer interface.
    '''

    def interface_writer(cls):
        '''Apply the ViewDisplayer interface on cls.'''

        if isinstance(add_view, basestring):
            cls.add_view = getattr(cls, add_view)
        else:
            cls.add_view = add_view
        if isinstance(close_view, basestring):
            cls.close_view = getattr(cls, close_view)
        else:
            cls.close_view = close_view
        return cls
    return interface_writer


class AbstractViewDisplayer(object):

    def __init__(self, mainwindow):
        raise NotImplementedError()

    def add_view(self, view):
        raise NotImplementedError()

    def close_view(self, view):
        raise NotImplementedError()
